#!/bin/bash

#Se chequea que todo este bien
if [ $# -ne "1" ]
then
	echo "USO: ${0} <CLIENT_NAME>"
	exit 0
fi

IP_DST="192.168.0.105"  # Cambiar por la IP fisica de la maquina con la topologia
echo "Usando ip fisica: ${IP_DST}"

CLIENT_NAME=${1}

source config.sh  # incluyo las constantes

if [ "${CLIENT_NAME}" == "WEBSERVER" ]
then
	CLIENT=("${WEBSERVER[@]}")

elif [ "${CLIENT_NAME}" == "FTPSERVER" ]
then
	CLIENT=("${FTPSERVER[@]}")

elif [ "${CLIENT_NAME}" == "TELSERVER1" ]
then
	CLIENT=("${TELSERVER1[@]}")

elif [ "${CLIENT_NAME}" == "TELSERVER2" ]
then
	CLIENT=("${TELSERVER2[@]}")

elif [ "${CLIENT_NAME}" == "HOSTA" ]
then
	CLIENT=("${HOSTA[@]}")

elif [ "${CLIENT_NAME}" == "HOSTB" ]
then
	CLIENT=("${HOSTB[@]}")

elif [ "${CLIENT_NAME}" == "HOSTC" ]
then
	CLIENT=("${HOSTC[@]}")

else
	echo "El cliente debe ser: 'WEBSERVER', 'FTPSERVER', 'TELSERVER1', 'TELSERVER2', 'HOSTA', 'HOSTB' O 'HOSTC'"
	exit 1
fi


TAP=${CLIENT[0]}
HOST_IP=${CLIENT[1]}
NETMASK=${CLIENT[2]}
PORT_NUM=${CLIENT[3]}

#Crea una interfaz "tap" ethernet que va a ser usada por el tunel
sudo openvpn --mktun --dev ${TAP}
sudo ifconfig ${TAP} 0.0.0.0 promisc up

#Abre el tunel hacia IP_DST en otra ventana
gnome-terminal --title=${CLIENT_NAME} -x sudo openvpn --remote ${IP_DST} --port ${PORT_NUM} --dev ${TAP} --ifconfig ${HOST_IP} ${NETMASK}

exit 0
